//
//  SlowBlood.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-6-19.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//
//  缓动三层血条,anchorPoint是(0.5, 0.5),适用不规则的血条

#import "SlowBlood.h"

@implementation SlowBlood
@synthesize width = _width;
@synthesize height = _height;

/**
	返回一个三层缓动血条的实例
	@param bgFrameName 背景图片的FrameName
	@param slowFrameName 缓动图片的FrameName
	@param bloodFrameName 血条图片的FrameName
	@returns 返回实例
 */
+(id) bloodWithBG:(NSString*)bgFrameName
             slow:(NSString*)slowFrameName
            blood:(NSString*)bloodFrameName
{
    return [[[self alloc] initWithBG:bgFrameName slow:slowFrameName blood:bloodFrameName] autorelease]; 
}

/**
	初始化一个三层缓动血条的实例
	@param bgFrameName 背景图片的FrameName
	@param slowFrameName 缓动图片的FrameName
	@param bloodFrameName 血条图片的FrameName
	@returns 返回实例
 */
-(id) initWithBG:(NSString*)bgFrameName
            slow:(NSString*)slowFrameName
           blood:(NSString*)bloodFrameName
{
    if( (self=[super init])) 
    {
        //血槽的背景
        _bgSprite = [CCSprite spriteWithSpriteFrameName:bgFrameName]; 
        [self addChild:_bgSprite];
        self.width =  _bgSprite.contentSize.width;
        self.height =  _bgSprite.contentSize.height;
        
        //缓动血条
        CCSprite *slowSprite = [CCSprite spriteWithSpriteFrameName:slowFrameName];
        _slowTimer = [CCProgressTimer progressWithTexture:[self sprite2Texture2D:slowSprite]];
        _slowTimer.type = kCCProgressTimerTypeHorizontalBarLR;
        _slowTimer.percentage = 99.9;
        _slowTimer.position = ccp(0, 0);
        [self addChild:_slowTimer];
        
        //血条
        CCSprite *bloodSprite = [CCSprite spriteWithSpriteFrameName:bloodFrameName];
        _bloodTimer = [CCProgressTimer progressWithTexture:[self sprite2Texture2D:bloodSprite]];
        _bloodTimer.type = kCCProgressTimerTypeHorizontalBarLR;
        _bloodTimer.percentage = 100;
        _bloodTimer.position = ccp(0, 0);
        [self addChild:_bloodTimer];
        
        [self schedule:@selector(secondUpdate) interval:4.0f];
    }
    
    return self;
}

/**
	设置血条的进度，0～100之间
	@param progress 进度，0～100之间
	@param duration 缓动持续的秒数
 */
-(void) setProgress:(int)progress duration:(ccTime)duration
{
    if(progress < 0 || progress > 100) return;

    //改变血条 
    _bloodTimer.percentage = progress;
    
    //改变缓动血条
    [_slowTimer runAction:[CCProgressTo actionWithDuration:duration percent:progress]];
}

/**
	将一个sprite转换成Texture2D
	@param sprite 
	@returns 
 */
-(CCTexture2D*) sprite2Texture2D:(CCSprite*)sprite
{
    CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];
    sprite.anchorPoint = ccp(0, 0);
    sprite.position = ccp(0, 0);
    sprite.flipY = YES;
    [renderTexture addChild:sprite];  
    [renderTexture begin];     
    [sprite draw]; // or [spr visit];
    [renderTexture end];
    
    CCTexture2D * result = renderTexture.sprite.texture;
    return result;
}

- (void)secondUpdate
{
    float HP = _bloodTimer.percentage;
    HP -= 10;
    [self setProgress:HP duration:2.0];
}

- (void) dealloc
{
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
